from pymongo import MongoClient

with open('../config.py', 'r', encoding="utf-8") as conf_r:
    exec(conf_r.read())

mongo_ip = mongoinfo['ip']
mongo_port = mongoinfo['port']
ip = mongoconfig["ip"]
port = mongoconfig["port"]
h_ip = mongoconfig["h_ip"]
h_port = mongoconfig["h_port"]
h_uname = mongoconfig["h_uname"]
h_pwd = mongoconfig["h_pwd"]
ovo_ip = mongoconfig["ovo_ip"]
ovo_port = mongoconfig["ovo_port"]
whether_do = mongoconfig["whether_do"]

client = MongoClient(host=mongo_ip, port=mongo_port)

config = client.guidang4config.config

D_id = "%s_%s_D" % (ip, port)
J_id = "%s_%s_J" % (ip, port)
A_id = "%s_%s_A" % (ip, port)

img = config.find_one({"_id": D_id})
if not img:
    D_data = {"_id": D_id, "ip": ip, "port": port, "repo": "D", "h_ip": h_ip[0], "h_port": h_port[0], "h_uname": h_uname[0],
              "h_pwd": h_pwd[0], "ovo_ip": ovo_ip, "ovo_port": ovo_port, "ID1": 1, "c1": 0, "m1": 30000, "ID2": 2,
              "c2": 0, "m2": 30000, "ID3": 3, "c3": 0, "m3": 30000, "now_id": "ID1", "whether_do": whether_do
              }

    J_data = {"_id": J_id, "ip": ip, "port": port, "repo": "J", "h_ip": h_ip[1], "h_port": h_port[1], "h_uname": h_uname[1],
              "h_pwd": h_pwd[1], "ovo_ip": ovo_ip, "ovo_port": ovo_port, "ID1": 4, "c1": 0, "m1": 30000, "ID2": 5,
              "c2": 0, "m2": 30000, "ID3": 6, "c3": 0, "m3": 30000, "now_id": "ID1", "whether_do": whether_do
              }

    A_data = {"_id": A_id, "ip": ip, "port": port, "repo": "A", "h_ip": h_ip[2], "h_port": h_port[2], "h_uname": h_uname[2],
              "h_pwd": h_pwd[2], "ovo_ip": ovo_ip, "ovo_port": ovo_port, "ID1": 7, "c1": 0, "m1": 30000, "ID2": 8,
              "c2": 0, "m2": 30000, "ID3": 9, "c3": 0, "m3": 30000, "now_id": "ID1", "whether_do": whether_do
              }

    config.insert_one(D_data)
    config.insert_one(J_data)
    config.insert_one(A_data)